<?php
declare (strict_types=1);

namespace app\admin\service\site;

use app\admin\model\data\Circle as CircleStatistic;
use app\admin\model\data\Course as CourseStatistic;
use app\admin\model\data\Paper as PaperStatistic;
use app\admin\model\data\PaymentOrderItem;
use app\admin\model\data\School as SchoolStatistic;
use app\admin\model\data\Student as StudentStatistic;
use app\admin\model\data\StudentVip;
use app\admin\model\data\Teacher as TeacherStatistic;
use app\common\helper\util\Timespan;
use app\common\service\BaseService;
use think\facade\Request;

/**
 * 后台首页接口
 */
class Index extends BaseService
{
    /**
     * 首页概况统计数据
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-13
     *
     * @return   [type]                         [description]
     */
    public function survey()
    {
        // // 总注册人数
        // $register_count_total = Account::count();
        // // 今日注册人数
        // $register_count_today = Account::whereDay('create_time', 'today')->count();
        // // 今日登录人数
        // $login_count_today = Account::whereDay('last_login_time', 'today')->count();
        //
        // return $this->parseToData([
        //     'register_count_total' => $register_count_total,
        //     'register_count_today' => $register_count_today,
        //     'login_count_today'    => $login_count_today,
        // ], 1);
        $todayTimespan     = [strtotime(date('Y-m-d')), Request::time()];
        $yesterdayTimespan = Timespan::getYesterdayTimespan();
        $totalTimespan     = [0, Request::time()];
        // 今日统计
        $today = [
            [
                'title'     => '销售额（元）',
                'today'     => PaymentOrderItem::getSalePriceByTimespan($todayTimespan),
                'yesterday' => PaymentOrderItem::getSalePriceByTimespan($yesterdayTimespan),
                'is_money'  => 1
            ],
            [
                'title'     => '订单量（单）',
                'today'     => PaymentOrderItem::getPaymentOrderCountByTimespan($todayTimespan),
                'yesterday' => PaymentOrderItem::getPaymentOrderCountByTimespan($yesterdayTimespan),
            ],
            [
                'title'     => '学员（人）',
                'today'     => StudentStatistic::getRegisterCountByTimespan($todayTimespan),
                'yesterday' => StudentStatistic::getRegisterCountByTimespan($yesterdayTimespan),
            ],
            [
                'title'     => '会员（人）',
                'today'     => StudentVip::getVipUserCountByTimespan($todayTimespan),
                'yesterday' => StudentVip::getVipUserCountByTimespan($yesterdayTimespan),
            ],
            [
                'title'     => '讲师（人）',
                'today'     => TeacherStatistic::getTeacherRegisterCountByTimespan($todayTimespan),
                'yesterday' => TeacherStatistic::getTeacherRegisterCountByTimespan($yesterdayTimespan),
            ],
            [
                'title'     => '课程（门）',
                'today'     => CourseStatistic::getCourseCountByTimespan($todayTimespan),
                'yesterday' => CourseStatistic::getCourseCountByTimespan($yesterdayTimespan),
            ],
            [
                'title'     => '动态（条）',
                'today'     => CircleStatistic::getCircleCountByTimespan($todayTimespan),
                'yesterday' => CircleStatistic::getCircleCountByTimespan($yesterdayTimespan),
            ],

        ];
        if (request()->is_platform) {
            // 平台访问: 统计机构数量
            $school  = [
                'title'     => '机构（个）',
                'today'     => SchoolStatistic::getSchoolRegisterCountByTimespan($todayTimespan),
                'yesterday' => SchoolStatistic::getSchoolRegisterCountByTimespan($yesterdayTimespan),
            ];
            $today[] = $school;
        } else {
            // 机构访问: 统计试卷
            $paper   = [
                'title'     => '试卷（套）',
                'today'     => PaperStatistic::getPaperCountByTimespan($todayTimespan),
                'yesterday' => PaperStatistic::getPaperCountByTimespan($yesterdayTimespan),
            ];
            $today[] = $paper;
        }

        // 累计统计
        $total = [
            [
                'title'    => '销售额（元）',
                'total'    => PaymentOrderItem::getSalePriceByTimespan($totalTimespan),
                'is_money' => 1
            ],
            [
                'title' => '订单量（单）',
                'total' => PaymentOrderItem::getPaymentOrderCountByTimespan($totalTimespan),
            ],
            [
                'title' => '学员（人）',
                'total' => StudentStatistic::getRegisterCountByTimespan($totalTimespan),
            ],
            [
                'title' => '会员（人）',
                'total' => StudentVip::getVipUserCountByTimespan($totalTimespan),
            ],

            [
                'title' => '讲师（人）',
                'total' => TeacherStatistic::getTeacherRegisterCountByTimespan($totalTimespan),
            ],
            [
                'title' => '课程（门）',
                'total' => CourseStatistic::getCourseCountByTimespan($totalTimespan),
            ],
            [
                'title' => '动态（条）',
                'total' => CircleStatistic::getCircleCountByTimespan($totalTimespan),
            ]

        ];

        if (request()->is_platform) {
            // 平台访问: 统计机构数量
            $school = [
                'title' => '机构（个）',
                'total' => SchoolStatistic::getSchoolRegisterCountByTimespan($totalTimespan),
            ];
            $total[] = $school;
        } else {
            // 机构访问: 统计试卷
            $paper   = [
                'title' => '试卷（套）',
                'total' => PaperStatistic::getPaperCountByTimespan($totalTimespan),
            ];
            $total[] = $paper;
        }

        return $this->parseToData([
            'update_time' => date('Y-m-d H:i', Request::time()),
            'today'       => $today,
            'total'       => $total
        ], 1);
    }
}
